package com.aistar.filter;

import com.aistar.pojo.Customer;
import com.aistar.service.CustomerService;
import com.aistar.service.impl.CustomerServiceImpl;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




public class AutoLoginFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1. session 中用户书否处于登录状态
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("loginCustomer");
        System.out.println("customer"+customer);
        //2.session 有用户信息，说明已登录，直接放行
        if(customer != null){
           return true;
        }else {//3. session 没有用户信息，说明未登录，找到request-cookie ---autoLogin
            Cookie[] cookies = request.getCookies();
            System.out.println("cookies" + cookies);
            if (cookies == null) {//（1）cookie==null, 必须先登录，跳转登录页面..
            response.sendRedirect(request.getContextPath() + "/customer/login.jsp");
                return false;
            } else { //（2)cookie!=null,获得cookie中的用户名与密码 ,再执行登录
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("customer")) {//用户名密码正确，执行自动登录，跳转到首页
                        System.out.println("有cookies");
                        String account = cookie.getValue().split("&")[0];
                        //分隔符产生的数组第0位和第一位
                        String password = cookie.getValue().split("&")[1];
                        CustomerService customerService = new CustomerServiceImpl();
                        Customer customerLogin = customerService.getByCookie(account, password);
                        if (customerLogin != null) { //用户名密码正确，执行自动登录，跳转到首页
                            System.out.println("用户名密码正确，执行自动登录，跳转到首页");
                            session.setAttribute("loginCustomer", customerLogin);
                            session.setAttribute("autoLoginName", customerLogin.getCustomerName());
//                            response.sendRedirect(request.getContextPath() + "/index.jsp");
                            return true;
                        } else {//用户名或密码错误，请重新登录，需要跳转到登录页面
                            System.out.println("用户名或密码错误，请重新登录，需要跳转到登录页面");
                            response.sendRedirect(request.getContextPath() + "/customer/login.jsp");
                            return false;
                        }
                    } else {
                        //用户未登录，cookie中没有自动登录，需要跳转到登录页面
                        System.out.println("用户未登录，cookie中没有自动登录，需要跳转到登录页面");
                        //modelAndView.setViewName("/login");
                        response.sendRedirect(request.getContextPath() + "/customer/login.jsp");
                        return false;
                    }
                }

            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}